package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.pojo.Item;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public PageDTO<Item> selectpage(long page, long size) {
        IPage<Item> pageinfo=new Page<>(page,size);
        LambdaQueryWrapper<Item>lqw=new LambdaQueryWrapper<>();
        itemMapper.selectPage(pageinfo,lqw);
        PageDTO<Item> pageDTO = new PageDTO<>();
        pageDTO.setTotal(pageinfo.getTotal());
        pageDTO.setList(pageinfo.getRecords());
        return pageDTO;
    }

    @Override
    public boolean updateStatus(long id, int status) {
        Item item = itemMapper.selectById(id);
        item.setStatus(status);
        int i = itemMapper.updateById(item);
        String exchangeName = "hmall.topic";
        if (status==1){
            rabbitTemplate.convertAndSend(exchangeName,"putaway",item);
        }else if (status==2){
            rabbitTemplate.convertAndSend(exchangeName,"soldout",item);
        }
        return i==1;
    }
}
